package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.Passive.AddStar;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.block.Blocks;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import java.util.Random;

public class RingAttack extends SkillBookType {

    public RingAttack() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 10 * 1000);

        boolean isAttacted = false;
        for (LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, playerEntity.getBoundingBox().inflate(5.0D, 5.0D, 5.0D))) {
            if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity) livingentity).isMarker()) && playerEntity.distanceToSqr(livingentity) < 18.0D) {
                livingentity.hurt(DamageSource.playerAttack(playerEntity), 10);
                AddStar.addCount(playerEntity);
                isAttacted=true;
            }
        }
        playerEntity.level.playSound(null, playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, playerEntity.getSoundSource(), 1.0F, 1.0F);
        playerEntity.sweepAttack();

        if (isAttacted) {
            playerEntity.jumpFromGround();
            SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
                @Override
                public void run() {
                    SkillUtils.sleep(200);
                    long time = System.currentTimeMillis();
                    while (System.currentTimeMillis()-time<5000) {
                        if (!playerEntity.level.getBlockState(playerEntity.blockPosition().below()).getBlock().is(Blocks.AIR)) {
                            for(LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, playerEntity.getBoundingBox().inflate(6.0D, 6.0D, 6.0D))) {
                                if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity)livingentity).isMarker()) && playerEntity.distanceToSqr(livingentity) < 36.0D) {
                                    livingentity.knockback(0.7F, MathHelper.sin(playerEntity.yRot * ((float) Math.PI / 180F)), -MathHelper.cos(playerEntity.yRot * ((float) Math.PI / 180F)));
                                    livingentity.addEffect(new EffectInstance(Effects.MOVEMENT_SLOWDOWN, 40, 10));
                                    livingentity.hurt(DamageSource.playerAttack(playerEntity), 10);
                                    AddStar.addCount(playerEntity);
                                }
                            }
                            Random random = new Random(System.currentTimeMillis());
                            playerEntity.sweepAttack();
                            playerEntity.level.playSound(null, playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), SoundEvents.BASALT_FALL, playerEntity.getSoundSource(), 1.0F, 1.0F);
                            break;
                        }
                    }
                }
            });
        }

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "ringAttact";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.2");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.2");
    }
}
